package com.hzqy.commons.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Pattern;

public class FileUtil {

	public static void inputFileString(String path, String content) throws IOException {
		FileWriter fileWritter = null;
		try{
			if(fileCheck(path,path)){
				File file = new File(path);
				judeFileExists(file);
				fileWritter = new FileWriter(file,false);
				fileWritter.write(content);

			}
		}catch (Exception e){

		}finally {
			if(fileWritter!=null){
				fileWritter.close();
			}
		}

	 }

	// 判断文件是否存在
	public static void judeFileExists(File file) {
       if (file.exists()) {
       } else {
            try {
               file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
           }
        }
    }
	private static Pattern[] PREVENT_XSS_INJECT_REGREPS = new Pattern[]{
			Pattern.compile("../"),
			Pattern.compile("..\\\\")
	};
	private static String[] fileTypes =  {"jpg","png","gif","xls","zip","mp4"};

	//过滤文件路径的特殊字符,对文件后缀判断
	public static boolean fileCheck(String filePath,String fileName){
		for (Pattern scriptPattern : PREVENT_XSS_INJECT_REGREPS) {
			if(scriptPattern.matcher(filePath).find()) {
				return false;
			}
		}
		String fileType=fileName.substring(fileName.length()-3);
		for (String type : fileTypes) {
			if(fileType.contains(type)){
				return true;
			}
		}
		return false;
	}

    public static void main(String arg[]) throws IOException {
		inputFileString("E:\\123.js","147");
	}
}
